import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';

import { gameActNoticeStatusArrs } from '../../status.pipe';
import { GameOperateManageService } from '../game-operate-manage.service';

@Component({
  selector: 'app-game-operate-notice-list',
  templateUrl: './game-operate-notice-list.component.html',
  styleUrls: ['./game-operate-notice-list.component.scss']
})
export class GameOperateNoticeListComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '标题', valueKey: 'title', value: '' },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'state',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...gameActNoticeStatusArrs],
    },
    { type: 3, labelText: '发送时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true },
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private msg: NzMessageService,
    private services: GameOperateManageService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getActivityListData(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * 发送 | 撤回 | 删除-
   * ----------------------------------------------------------------
   */

  // 发送
  sendItemData(item: any) {
    let params = {
      id: item.id,
      state: 1,
      isSendOut: 1
    }
    this.changeStateInterface(params);
  }

  // 撤回
  withdrawItemData(item: any) {
    let params = {
      id: item.id,
      state: 2
    }
    this.changeStateInterface(params);
  }

  // 删除
  deleteItemData(item: any) {
    let params = {
      id: item.id,
      deleteState: 1
    }
    this.changeStateInterface(params);
  }

  async changeStateInterface(params: any) {
    const {code, data, message}: any = await this.services.updateActivityNoticeData(params);
    if (code === 0) {
      this.msg.success('操作成功');
      this.loadTable();
    }
  }
}
